package com.yjy.fuyao.stu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjy.fuyao.stu.entity.Teacher;
import com.yjy.fuyao.stu.entity.query.TeacherQuery;
import com.yjy.fuyao.stu.service.TeacherService;
import com.yjy.fuyao.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author yjy
 * @since 2021-08-12
 */
@Api(tags = "讲师管理")
//@CrossOrigin
@RestController
@RequestMapping("/admin/stu/teacher")
public class AdminTeacherController {

    @Resource
    private TeacherService teacherService;

    @ApiOperation("获取列表")
    @GetMapping("/list")
    public R listAll(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("list",list);
    }

    @ApiOperation("逻辑删除")
    @DeleteMapping("/remove")
    public R removeById(
            @ApiParam("id")
            @RequestParam("id") String id){
        boolean result = teacherService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("分页显示讲师信息")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                    TeacherQuery teacherQuery){
        System.out.println("teacherQuery:================="+teacherQuery);
        Page<Teacher> pageParam = new Page<>(page, limit);
        IPage<Teacher> pageModel = teacherService.listPage(pageParam,teacherQuery);
        return R.ok().data("pageModel",pageModel);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher){

        boolean result = teacherService.save(teacher);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){

        Teacher teacher = teacherService.getById(id);
        return R.ok().data("record", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if(result){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

}

